// player.js - 玩家类
export default class Player {
    constructor(gameWidth, gameHeight) {
        this.gameWidth = gameWidth
        this.gameHeight = gameHeight
        this.width = 30
        this.height = 30
        this.x = gameWidth / 2 - this.width / 2
        this.y = gameHeight / 2 - this.height / 2
        this.speed = 2
        this.maxSpeed = 5
        this.color = '#4cc9f0'
    }

    // 在player.js中修改draw方法
    draw(context, camera) {
        // 世界坐标 → 屏幕坐标
        const screenX = this.x - camera.x
        const screenY = this.y - camera.y

        // 绘制玩家身体
        context.fillStyle = this.color
        context.fillRect(screenX, screenY, this.width, this.height)

        // 绘制玩家眼睛
        context.fillStyle = 'white'
        context.fillRect(screenX + this.width - 10, screenY + 7, 5, 5)
        context.fillRect(screenX + this.width - 10, screenY + this.height - 12, 5, 5)
    }

    update(input, deltaTime) {
        // 水平移动
        if (input.keys['ArrowLeft'] || input.keys['a'] || input.keys['A']) {
            this.x -= this.speed
        } else if (input.keys['ArrowRight'] || input.keys['d'] || input.keys['D']) {
            this.x += this.speed
        }

        // 垂直移动
        if (input.keys['ArrowUp'] || input.keys['w'] || input.keys['W']) {
            this.y -= this.speed
        } else if (input.keys['ArrowDown'] || input.keys['s'] || input.keys['S']) {
            this.y += this.speed
        }

        // 边界检查
        if (this.x < 0) this.x = 0
        if (this.x > this.gameWidth - this.width) this.x = this.gameWidth - this.width
        if (this.y < 0) this.y = 0
        if (this.y > this.gameHeight - this.height) this.y = this.gameHeight - this.height
    }
}